<?php

namespace app\admin\service;

use app\admin\model\BoxArticleComment;
use app\qsapi\model\Member;

class BoxArticleCommentService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxArticleComment();
    }

    public function getList(): array
    {
        $param = request()->param();
        $map   = [];
        if (isset($param['article_id']) && !empty($param['article_id'])) {
            $map[] = ['box_article_id', '=', $param['article_id']];
        }

        if (isset($param['pid']) && !empty($param['pid'])) {
            $map[] = ['pid', '=', $param['pid']];
        } else {
            $map[] = ['pid', '=', 0];
        }
        if (isset($param['member_id']) && !empty($param['member_id'])) {
            $map[] = ['create_user', '=', $param['member_id']];
        }
        if (isset($param['began_time']) && !empty($param['began_time'])) {
            $map[] = ['create_time', '>=', strtotime($param['began_time'])];
        }
        if (isset($param['end_time']) && !empty($param['end_time'])) {
            $map[] = ['create_time', '<=', strtotime($param['end_time'])];
        }
        if (isset($param['level'])) {
            $map[] = ['pid', '=', $param['level']];
        }
        if (isset($param['time']) && !empty($param['time'])) {
            $map[] = ['create_time', '>=', strtotime($param['time'][0])];
            $map[] = ['create_time', '<=', strtotime($param['time'][1])];
        }

        $result        = $this->model->where($map)->order('id desc')->where('mark', '=', 1)->paginate($param['limit'] ?? 10)->toArray();
        $data['count'] = $result['total'] ?? 0;
        $data['data']  = $result['data'] ?? [];
        if ($data['count'] > 0) {
            $member_ids   = array_column($data['data'], 'create_user');
            $member_info = (new Member())->field('id,nickname,avatar')->whereIn('id', $member_ids)->select();
            $member_info = array_column($member_info ? $member_info->toArray() : [], null, 'id');
            foreach ($data['data'] as $k => $item) {
                $data['data'][$k]['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                $data['data'][$k]['member_info'] = $member_info[$item['create_user']] ?? [];
            }
        }
        return array (
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $data['data'],
            "count" => $data['count'],
        );
    }

    public function delete(): array
    {
        $param = request()->param();
        if (isset($param['id']) && !empty($param['id'])) {
            $map[] = ['id', '=', $param['id']];
            //0=待审核;1=正常;2=不通过;3=删除;
            $result = $this->model->where($map)->update(['status' => 3]);
            if ($result) {
                return message('删除成功', true);
            } else {
                return message('删除失败', false);
            }
        } else {
            return api_message([], '参数错误');
        }
    }

}